import sqlite3
from typing import List

class WordDatabase:
    def __init__(self, db_name: str = "words.db"):
        self.conn = sqlite3.connect(db_name)
        self.create_table()
    
    def create_table(self):
        cursor = self.conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS words (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                word TEXT UNIQUE,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ''')
        self.conn.commit()
    
    def add_word(self, word: str) -> bool:
        try:
            cursor = self.conn.cursor()
            cursor.execute("INSERT OR IGNORE INTO words (word) VALUES (?)", (word.lower(),))
            self.conn.commit()
            return cursor.rowcount > 0
        except Exception as e:
            print(f"Error adding word: {e}")
            return False
    
    def add_words(self, words: List[str]) -> int:
        added_count = 0
        for word in words:
            if self.add_word(word):
                added_count += 1
        return added_count
    
    def get_all_words(self) -> List[str]:
        cursor = self.conn.cursor()
        cursor.execute("SELECT word FROM words ORDER BY word")
        return [row[0] for row in cursor.fetchall()] 